package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.Discover;
import com.innovation.chengzhangriji.Html5;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.widget.CuteButton;

public class FindFragment extends BaseFragment implements View.OnClickListener {

    ImageView imageNewPoint;

    private RelativeLayout btnMessage;

    private CuteButton btnSqsd, btnJsd, btnBbzw, btnRwdr, btnHdgc, btnCzsc;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(contentView == null) {
            contentView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_find, null);
            setLayout();
        }
        return contentView;
    }

    // 初始化视图
    private void setLayout() {
        imageNewPoint = (ImageView) contentView.findViewById(R.id.icon_new);

        // 消息中心
        btnMessage = (RelativeLayout) contentView.findViewById(R.id.btn_message);
        btnMessage.setOnClickListener(this);
        // 神奇时代
        btnSqsd = (CuteButton) contentView.findViewById(R.id.btn_sqsd);
        btnSqsd.setOnClickListener(this);
        // 旧时代
        btnJsd = (CuteButton) contentView.findViewById(R.id.btn_jsd);
        btnJsd.setOnClickListener(this);
        // 宝贝租屋
        btnBbzw = (CuteButton) contentView.findViewById(R.id.btn_bbzw);
        btnBbzw.setOnClickListener(this);
        // 任务达人
        btnRwdr = (CuteButton) contentView.findViewById(R.id.btn_task);
        btnRwdr.setOnClickListener(this);
        // 活动广场
        btnHdgc = (CuteButton) contentView.findViewById(R.id.btn_activity);
        btnHdgc.setOnClickListener(this);
        // 成长商城
        btnCzsc = (CuteButton) contentView.findViewById(R.id.btn_market);
        btnCzsc.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        showNewMsgPoint();
    }

    // 检查是有新评论或日记标记
    public void showNewMsgPoint() {
        if(imageNewPoint == null) return;
        if(Config.getHasNewComment(getContext()) || Config.getHasNewMessage(getContext())) {
            imageNewPoint.setVisibility(View.VISIBLE);
        } else {
            imageNewPoint.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        if(v.getId() == R.id.btn_message) {
            intent.setClassName(getContext(), DiscoverActivity.class.getName());
        } else {
            intent.setClassName(getContext(), Html5Activity.class.getName());
            if(v.getId() == R.id.btn_sqsd) {
                intent.putExtra("title", getString(R.string.shenqishidai));
                intent.putExtra("url", Html5.FIND_SHENQISHIDAI);
            } else if(v.getId() == R.id.btn_jsd) {
                intent.putExtra("title", getString(R.string.jiushidai));
                intent.putExtra("url", Html5.FIND_JIUSHIDAI);
            } else if(v.getId() == R.id.btn_bbzw) {
                intent.putExtra("title", getString(R.string.baobeizuwu));
                intent.putExtra("url", Html5.FIND_BAOBEIZUWU);
            } else if(v.getId() == R.id.btn_task) {
                intent.putExtra("title", getString(R.string.task));
                intent.putExtra("url", Html5.FIND_RENWUDAREN);
            } else if(v.getId() == R.id.btn_activity) {
                intent.putExtra("title", getString(R.string.activity));
                intent.putExtra("url", Html5.FIND_HUODONGGUANGCHANG);
            } else if(v.getId() == R.id.btn_market) {
                intent.putExtra("title", getString(R.string.market));
                intent.putExtra("url", Html5.FIND_CHENGZHANGSHANGCHENG);
            }
        }
        startActivity(intent);
    }
}
